package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.PermissionBO;
import com.project2.bean.po.*;
import com.project2.bean.vo.*;
import com.project2.mapper.MarketPermissionMapper;
import com.project2.mapper.MarketRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/12 19:29
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Override
    public CommonData<RoleOptionsVo> options() {
//        // 分页插件
//        PageHelper.startPage(1, 3);
//        MarketRoleExample marketRoleExample = new MarketRoleExample();
//        // 查询MarketRole
//        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
//
//        List<RoleOptionsVo> roleOptionsVoList=new ArrayList<>();
//        // 为RoleOptionsVo赋值
//        for (MarketRole marketRole : marketRoles) {
//            RoleOptionsVo roleOptionsVo = new RoleOptionsVo();
//            // 赋值
//            roleOptionsVo.setValue(marketRole.getId());
//            roleOptionsVo.setLabel(marketRole.getName());
//            // 加入列表
//            roleOptionsVoList.add(roleOptionsVo);
//        }
//        PageInfo<RoleOptionsVo> pageInfo = new PageInfo<>(roleOptionsVoList);

        List<RoleOptionsVo> roles = marketRoleMapper.selectAllRole();
        PageInfo<RoleOptionsVo> pageInfo = new PageInfo<>(roles);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketRole> list(String name, PageBo bo) {
        // 分页
        PageHelper.startPage(bo.getPage(), bo.getLimit());
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);// 展示未删除的数据
        // 判断是否为空 根据姓名查找
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        // 排序
        example.setOrderByClause(bo.getSort() + " " + bo.getOrder());

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        PageInfo<MarketRole> pageInfo = new PageInfo<>(marketRoles);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketRole create(MarketRole marketRole) {


        Date date = new Date();
        marketRole.setAddTime(date);
        marketRole.setUpdateTime(date);
        marketRole.setDeleted(false);
        marketRole.setEnabled(true);
        marketRoleMapper.insert(marketRole);
        return marketRole;
    }

    @Override
    public int update(MarketRole marketRole) {
        marketRole.setUpdateTime(new Date());
        int affectedRow = marketRoleMapper.updateByPrimaryKey(marketRole);
        return affectedRow;
    }

    @Override
    public int delete(MarketRole marketRole) {
        marketRole.setDeleted(true);
        int affectedRow = marketRoleMapper.deleteByPrimaryKey(marketRole.getId());
        return affectedRow;
    }

    @Override
    public PermissionListVO getPermissions(String roleId) {
        // PermissionListVO-两个list: List<PermissionVO> systemPermissions ； List<String> assignedPermissions
        PermissionListVO permissionListVO = new PermissionListVO();

        // List<PermissionVO> systemPermissions 所有模块权限
        // 第一层
        //  PermissionVO 👉 id ,label, List<PermissionChildrenVO>
        List<PermissionVO> systemPermissions = new ArrayList<>();
        //查询
        List<PermissionPO> permissionPOS = marketRoleMapper.queryPermissionByPid(0);
        for (PermissionPO permissionPO : permissionPOS) {
            PermissionVO permissionVO = new PermissionVO();
            // 为VO赋值
            permissionVO.setId(permissionPO.getName());
            permissionVO.setLabel(permissionPO.getLabel());
            // 第二层
            // PermissionChildrenVO →id ,label, List<PermissionChildrenChildrenVO>
            List<PermissionChildrenVO> permissionChildrenVOS = new ArrayList<>();
            // 查询 用第一层的id对应第二层的pid
            List<PermissionPO> permissionPOS2 = marketRoleMapper.queryPermissionByPid(permissionPO.getId());
            for (PermissionPO po : permissionPOS2) {
                PermissionChildrenVO permissionChildrenVO = new PermissionChildrenVO();
                permissionChildrenVO.setId(po.getName());
                permissionChildrenVO.setLabel(po.getLabel());
                // 第三层
                // PermissionChildrenChildrenVO → id ,label, api
                List<PermissionChildrenChildrenVO> permissionChildrenChildrenVOS = new ArrayList<>();
                // 查询 用第二层的id对应第三层的pid
                List<PermissionPO> permissionPOS3 = marketRoleMapper.queryPermissionByPid(po.getId());
                for (PermissionPO po2 : permissionPOS3) {
                    PermissionChildrenChildrenVO permissionChildrenChildrenVO = new PermissionChildrenChildrenVO();
                    permissionChildrenChildrenVO.setId(po2.getName());
                    permissionChildrenChildrenVO.setLabel(po2.getLabel());
                    permissionChildrenChildrenVO.setApi(po2.getApi());
                    permissionChildrenChildrenVOS.add(permissionChildrenChildrenVO);
                }
                permissionChildrenVO.setChildren(permissionChildrenChildrenVOS);
                permissionChildrenVOS.add(permissionChildrenVO);
            }
            permissionVO.setChildren(permissionChildrenVOS);
            systemPermissions.add(permissionVO);
        }
        permissionListVO.setSystemPermissions(systemPermissions);


        //assignedPermissions roleId对应管理员的三级权限
        List<String> list2 = marketRoleMapper.queryPermissionByRoleId(roleId);
        // 超级管理员对应为* 所有权限,只能去权限表查找
        if ("*".equals(list2) && list2.size() == 1) {
            List<String> stringList = marketRoleMapper.queryApiByType(2);
            permissionListVO.setAssignedPermissions(stringList);
        } else {
            permissionListVO.setAssignedPermissions(list2);
        }

        return permissionListVO;
    }

    @Override
    public int updatePermissions(PermissionBO bo) {

        // 先删除原来的
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(bo.getRoleId());
        marketPermissionMapper.deleteByExample(example);
        // 插入
        List<String> permissions = bo.getPermissions();
        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setRoleId(bo.getRoleId());
            marketPermission.setPermission(permission);
            Date date = new Date();
            marketPermission.setAddTime(date);
            marketPermission.setUpdateTime(date);
            marketPermission.setDeleted(false);
            int affectedRows = marketPermissionMapper.insert(marketPermission);
        }
        return 1;
    }

}
